//
// Created by Tony on 2024/5/27.
//

#ifndef PARSER_EXPR_PARSER_H
#define PARSER_EXPR_PARSER_H

#include "AbsParser.h"
#include "ast/Ast.h"
#include <cstdint>
#include <unordered_map>

namespace Parser {

enum class Assoc { LEFT = 0, RIGHT = 1 };

struct OpPrecedence {
    uint16_t value;
    Assoc assoc;
};

class ExprParser : public AbsParser {
    static std::unordered_map<string, OpPrecedence> OP_TABLE;

    static void initOpPrecedence();

public:
    ExprParser();

    shared_ptr<Ast> parse() override;

protected:
    static bool isOperator(const shared_ptr<Lexer::Token>& token);
    static bool rightIsExpr(OpPrecedence curOp, OpPrecedence nextOp);
    shared_ptr<Ast> shiftRight(const shared_ptr<Ast>& left, OpPrecedence nextOpOfLeft);
    shared_ptr<Ast> expr() override;
};
} // namespace Parser

#endif // PARSER_EXPR_PARSER_H
